package com.irdstudio.efp.esb.service.client.impl;

import com.baidu.disconf.client.common.annotations.DisconfItem;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.esb.common.client.req.EsbReqAppHeadBean;
import com.irdstudio.efp.esb.common.client.req.EsbReqSysHeadBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.InstitutionEnums;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.*;

/**
 * 需求编号：优e贷<br/>
 * 问题编号：<br/>
 * 功能描述：<br/>
 * 开发人员：dengqiwen<br/>
 * 创建时间：2020/8/4 17:22
 */
@Component("YEDESBBeanCreator")
public class YEDESBBeanCreator extends DefaultESBBeanCreator {
        private static final DefaultESBBeanCreator INSTANCE = new DefaultESBBeanCreator();
        private static final PatternDescriptor pattern =
                DefaultPatternDescriptor.of(ESBClientConstance.CnsmrSysNo +
                        YEAR_2 + MONTH + DAY + HOUR + MIN + SECOND + SEQ, 6);

        private static DateTimeFormatter dateForm = DateTimeFormatter.ofPattern("yyyyMMdd");
        private static DateTimeFormatter timeForm = DateTimeFormatter.ofPattern("HHmmss");


    @Value("${esb.client.yed.usrno}")
    private String usrno;

    private String msgTp;

    @PostConstruct
    public void test () {
        System.out.println(usrno);
    }

    @DisconfItem(key = "esb.client.yed.usrno", associateField = "usrno")
    public String getUsrno() {
        return usrno;
    }

    public void setUsrno(String usrno) {
        this.usrno = usrno;
    }

    public String getMsgTp() {
        return msgTp;
    }

    public void setMsgTp(String msgTp) {
        this.msgTp = msgTp;
    }

    @Override
        protected EsbReqAppHeadBean buildAppHead() {
            EsbReqAppHeadBean appHead = new EsbReqAppHeadBean();
            String instId = TraceUtil.getInstId() == null ? InstitutionEnums.InstitutionEnum.ZHYYB.getCode():TraceUtil.getInstId();
            appHead.setInstId(instId);
            appHead.setCnlTp(ESBClientConstance.CnlTp);
            appHead.setUsrNo(usrno);

            appHead.setUsrPswd("");
            appHead.setUsrLvl("");
            appHead.setUsrTp("");
            appHead.setTlrSrlNo("");
            appHead.setCnlDtlTp("");
            appHead.setCnsmrTxnCd("");
            return appHead;
        }
        @Override
        protected EsbReqSysHeadBean buildSysHead(String serverCode, String scene) throws Exception {
            EsbReqSysHeadBean syshead = new EsbReqSysHeadBean();
            syshead.setSvcCd(serverCode); // 要调用的服务代码
            syshead.setScnCd(scene); // 服务的应用场景
            syshead.setCnsmrSysID(ESBClientConstance.CnsmrSysNo); // 网贷的系统id
            syshead.setOrgnlCnsmrSysID(ESBClientConstance.CnsmrSysNo); // 网贷的系统id
            String consumerSrlNo = generateESBTradeSerialNo();
            String globalSrlNo = TraceUtil.getTraceId();
            syshead.setGlblSrlNo(globalSrlNo == null ? consumerSrlNo : globalSrlNo);
            syshead.setCnsmrSrlNo(consumerSrlNo);
            LocalDateTime date = LocalDateTime.now();
            syshead.setTxnDt(dateForm.format(date));
            syshead.setTxnTm(timeForm.format(date));
            syshead.setTxnMd(ESBClientConstance.TxnMd.ONLINE.name());
            syshead.setUsrLng(ESBClientConstance.UsrLng.CHINESE.name());
            syshead.setFileFlg(ESBClientConstance.FileFlg.NONE.VALUE);
            syshead.setEryptMd(ESBClientConstance.EryptMd.NONE.VALUE);
            syshead.setPlafmID(TraceUtil.getPlafmID());
            syshead.setPlafmUsrID(TraceUtil.getPlafmUsrID());
            syshead.setCompany(ESBClientConstance.Company);


            syshead.setOrgnlTmlIdNo("");
            syshead.setTmlIdNo("");
            syshead.setOrgnlCnsmrSvcNo("");
            syshead.setCnsmrSvcNo("");
            syshead.setMACVal("");
            syshead.setMACFctr("");
            syshead.setPINSd("");
            syshead.setSvcVerNo("");
            syshead.setVerfFlg("");
            syshead.setMsgTp(msgTp);
            syshead.setPltfmClntNo("");
            syshead.setPrdctID("");
            syshead.setRelGlblSrlNo("");
            return syshead;
        }

        /* 生成ESB系统的交易流水号(CnsmrSrlNo) */
        private String generateESBTradeSerialNo() throws Exception {
            PatternedLimitableSeqService sequenceService = SpringContextUtils.getBean(PatternedLimitableSeqService.class);
            return sequenceService.getPatternedSequence("ESB", pattern);
        }
}
